package com.yyplatform.admin.platform.role.service.impl;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.platform.role.model.dto.SysUserTokenDTO;
import com.yyplatform.admin.platform.role.model.dto.SysUserTokenQueryDTO;
import com.yyplatform.admin.platform.role.model.entity.SysUserTokenEntity;
import com.yyplatform.admin.platform.role.repository.SysUserTokenRepository;
import com.yyplatform.admin.platform.role.service.SysUserTokenService;
import com.yyplatform.admin.infra.utils.DateUtils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
@Service
public class SysUserTokenServiceImpl implements SysUserTokenService {

    private SysUserTokenRepository sysUserTokenRepository;
    private ModelMapper modelMapper;

    @Override
    public Page<SysUserTokenDTO> query(SysUserTokenQueryDTO queryDTO) {

        Page<SysUserTokenEntity> entityPage =
                sysUserTokenRepository.findPage(queryDTO.getUsername(),
                        DateUtils.getBeginDate(queryDTO.getCreateTimeArr()),
                        DateUtils.getEndDate(queryDTO.getCreateTimeArr()),
                        queryDTO.getPageRequestOrderByIdDesc());

        List<SysUserTokenDTO> list = entityPage.stream()
                .map(e -> modelMapper.map(e, SysUserTokenDTO.class))
                .toList();

        return new PageImpl<>(list, entityPage.getPageable(), entityPage.getTotalElements());

    }

    @Override
    public void delete(Long id) {
        sysUserTokenRepository.deleteById(id);
    }

    @Override
    public void deleteAll() {
        sysUserTokenRepository.deleteAll();
    }
}
